package com.gxa.controller;

import com.gxa.pojo.Log;
import com.gxa.service.LogService;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/log")
@CrossOrigin
public class LogController {

    @Resource
    LogService logService;

    JsonResult jsonResult;

    @ResponseBody
    @RequestMapping("/logPage")
    public Map findLogByPage(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        String number = map.get("number").toString();
        String size = map.get("size").toString();
        Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
        Integer limit=Integer.parseInt(size);
        List<Log> logs = logService.findLogByPage(offset, limit);
        Integer count = logService.findLogAll();
        jsonResult.setData(logs);
        jsonResult.put("count",count);
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/logAdd")
    public Map insertLog(@RequestBody Map map){
        jsonResult=new JsonResult();
        Date date=new Date();
        map.put("operationTime",date);
        int i = logService.insertLog(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差了");
        }
        return jsonResult.getValues();
    }

    // 批量删除
    @ResponseBody
    @RequestMapping(value = "/deleteLogs")
    public Map deleteBatch(@RequestBody Map<String,List> s){
        List ids = s.get("logID");
        int i = logService.deleteLogs(ids);
        JsonResult jsonResult = new JsonResult();
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功！");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差了！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/logSeach")
    public Map selectLogByNameAndTime(@RequestBody Map map) throws ParseException {
        jsonResult=new JsonResult();
        System.out.println(map);
        Integer count = logService.findLogAllS(map);
        String number=map.get("number").toString();
        String size = map.get("size").toString();
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit= Integer.valueOf(size);
        map.put("offset",offset);
        map.put("limit",limit);
        System.out.println(map);
        List<Log> logs = logService.selectLogByNameAndTime(map);
        System.out.println(logs);
        if (logs!=null){
            jsonResult.setData(logs);
            jsonResult.put("count",count);
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差！！");
        }
        return jsonResult.getValues();
    }

}